package constant.milk.toilet;

import constant.milk.toilet.database.DbAdapter;
import constant.milk.toilet.util.MilkAd;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class SuperActivity extends Activity
{
	public float TEXT_SIZE; 
	public static final int TEXT_COLOR = 0xff3d3d3d; 
	
	public Context superContext = this;
	
	public DbAdapter superDb;
	
	private ViewGroup bodyLayout;
	
	private ViewGroup superAdView;
	
	private boolean isAdFlag = true;
	
	public String shape;
	
	private MilkAd ad;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		super.setContentView(R.layout.super_activity);
		
		bodyLayout = (ViewGroup) findViewById(R.id.bodyLayout);
		
		superAdView = (ViewGroup) findViewById(R.id.superAdview);
		
		superDb = DbAdapter.getInstance();
		
		setSettingFontSize();
		
		if (isAdFlag)
		{
			ad = new MilkAd(superContext, (LinearLayout) superAdView);
		}
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		if (ad != null)
		{
			ad.resumeAd();
		}
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		
		if (ad != null)
		{
			ad.destoryAd();
		}
	}
	
	@Override
	public void setContentView(int layoutResID)
	{
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(layoutResID, null);
		this.setContentView(view);
	}
	
	@Override
	public void setContentView(View view) {
		this.bodyLayout.removeAllViews();
		
		if(view == null)
		{
			return;
		}
		
		ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
		
		if(layoutParams == null)
		{
			layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		}
		else
		{
			layoutParams.width = LayoutParams.MATCH_PARENT;
			layoutParams.height = LayoutParams.MATCH_PARENT;
		}
		
		view.setLayoutParams(layoutParams);
		
		this.bodyLayout.addView(view);
		
		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		setGlobalFont(root);
	}
	
	@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);
		
		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		setGlobalFont(root);
	}
	
	private void setGlobalFont(ViewGroup root) 
	{
	    for (int i = 0; i < root.getChildCount(); i++) 
	    {
	        View child = root.getChildAt(i);
	        
	        if (child instanceof TextView)
	        {
	        	((TextView)child).setTextSize(TEXT_SIZE);
	        	((TextView)child).setTextColor(TEXT_COLOR);
	        }
	        else if (child instanceof EditText)
			{
	        	((EditText)child).setTextSize(TEXT_SIZE);
	        	((EditText)child).setTextColor(TEXT_COLOR);
			}
	        else if (child instanceof Button)
			{
	        	((Button)child).setTextSize(TEXT_SIZE);
	        	((Button)child).setTextColor(TEXT_COLOR);
			}
	        else if (child instanceof ViewGroup)
	        {
	        	setGlobalFont((ViewGroup)child);
	        }
	    }
	}
	
	protected void setSettingFontSize()
	{
		int fontSize = 9;
		
		SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int fontFlag = p.getInt(getString(R.string.prefFont), 1);
		
		if (fontFlag == 0) fontSize = 12;
		else if (fontFlag == 1) fontSize = 16;
		else if (fontFlag == 2) fontSize = 20;
		
		TEXT_SIZE = fontSize;
	}

	/**
	 * xml의 아이디 값을 매칭 시킨다.
	 */
	protected abstract void setFindViewId();
	
	/**
	 * 아이디에 클릭리스너를 달아준다.
	 */
	protected abstract void setOnClick();
	
	/**
	 * 데이터를 셋팅한다.
	 */
	protected abstract void setData();

	public boolean isAdFlag()
	{
		return isAdFlag;
	}

	public void setAdFlag(boolean isAdFlag)
	{
		this.isAdFlag = isAdFlag;
	}
}
